package cn.iocoder.yudao.module.enterprise.controller.admin.praxisProjectResult;

import cn.hutool.core.lang.Assert;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.enterprise.controller.admin.praxisProjectResult.vo.EnterpriseProjectResultCreateReqVO;
import cn.iocoder.yudao.module.enterprise.controller.admin.praxisProjectResult.vo.EnterpriseProjectResultPageReqVO;
import cn.iocoder.yudao.module.enterprise.controller.admin.praxisProjectResult.vo.EnterpriseProjectResultRespVO;
import cn.iocoder.yudao.module.enterprise.controller.admin.praxisProjectResult.vo.EnterpriseProjectResultUpdateReqVO;
import cn.iocoder.yudao.module.enterprise.service.praxisProjectResult.EnterpriseProjectResultService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Api(tags = "管理后台 -企业端- 项目结果公示")
@RestController
@RequestMapping("/enterprise/project-result")
@Validated
public class EnterpriseProjectResultController {

    @Resource
    private EnterpriseProjectResultService enterpriseProjectResultService;

    @PostMapping("/create")
    @ApiOperation("创建项目结果公示")
    public CommonResult<Long> createPraxisProjectResult(@Valid @RequestBody EnterpriseProjectResultCreateReqVO createReqVO) {
        Assert.notNull(createReqVO.getProjectId(), "项目不能为空");
        createReqVO.setStatus(0);
        createReqVO.setExamineFlag(0);
        createReqVO.setCreatorRole("企业");
        return success(enterpriseProjectResultService.createPraxisProjectResult(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新项目结果公示")
    public CommonResult<Boolean> updatePraxisProjectResult(@Valid @RequestBody EnterpriseProjectResultUpdateReqVO updateReqVO) {
        Assert.notNull(updateReqVO.getProjectId(), "项目不能为空");
        if(null!=updateReqVO.getStatus() && 2==updateReqVO.getStatus()){
            return error("已发布不能编辑");
        }
        if(null==updateReqVO.getStatus() || 0==updateReqVO.getStatus()){
            updateReqVO.setExamineFlag(0);
            updateReqVO.setStatus(0);
            updateReqVO.setExamineStatus(null);
            updateReqVO.setExamineNotes("");
        }
        enterpriseProjectResultService.updatePraxisProjectResult(updateReqVO);
        return success(true);
    }

    @PutMapping("/release")
    @ApiOperation("发布项目结果公示")
    public CommonResult<Boolean> release(@Valid @RequestBody EnterpriseProjectResultUpdateReqVO updateReqVO) {
        updateReqVO.setStatus(1);
        enterpriseProjectResultService.release(updateReqVO);
        return success(true);
    }


    @TenantIgnore
    @DeleteMapping("/delete")
    @ApiOperation("删除项目结果公示")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    public CommonResult<Boolean> deletePraxisProjectResult(@RequestParam("id") Long id) {
        enterpriseProjectResultService.deletePraxisProjectResult(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得项目结果公示")
    @TenantIgnore
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<EnterpriseProjectResultRespVO> getPraxisProjectResult(@RequestParam("id") Long id) {
        EnterpriseProjectResultRespVO p = enterpriseProjectResultService.getPraxisProjectResult(id);
        return success(p);
    }


    @PostMapping("/page")
    @ApiOperation("获得项目结果公示分页")
    @TenantIgnore
    public CommonResult<PageResult<EnterpriseProjectResultRespVO>> getPraxisProjectResultPage(@Valid @RequestBody EnterpriseProjectResultPageReqVO pageVO) {
        PageResult<EnterpriseProjectResultRespVO> pageResult = enterpriseProjectResultService.getPraxisProjectResultPage(pageVO);
        return success(pageResult);
    }

}
